package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.opensource.svgaplayer.SVGADrawable;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;

/**
 * Created by TCL on 2019/5/31.
 * Describe:
 */
public class SVGAActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rl_layout)
    RelativeLayout rlLayout;
    @BindView(R.id.ll_layout)
    LinearLayout llLayout;
    @BindView(R.id.animationView)
    SVGAImageView animationView;
//    SVGAImageView animationView = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_svga;
    }

    @Override
    protected void initData() {
//        animationView = new SVGAImageView(this);
        animationView.setBackgroundColor(getResources().getColor(R.color.color_WHITE));
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_svga));
        toolbar.inflateMenu(R.menu.menu_svga);

        loadAnimation(0);
//        setContentView(animationView);
//        rlLayout.addView(animationView);
//        llLayout.addView(animationView);
    }

    @Override
    protected void setListener() {
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int id = item.getItemId();
                if(id == R.id.toolbar_menu){
                    return true;
                }else{
                    switch (id) {
                        case R.id.toolbar2:
                            loadAnimation(2);
                            break;
                        case R.id.toolbar3:
                            loadAnimation(3);
                            break;
                        case R.id.toolbar4:
                            loadAnimation(4);
                            break;
                        case R.id.toolbar5:
                            loadAnimation(5);
                            break;
                        case R.id.toolbar6:
                            loadAnimation(6);
                            break;
                        case R.id.toolbar7:
                            loadAnimation(7);
                            break;
                        case R.id.toolbar8:
                            loadAnimation(8);
                            break;
                        case R.id.toolbar9:
                            loadAnimation(9);
                            break;
                        case R.id.toolbar10:
                            loadAnimation(10);
                            break;
                        case R.id.toolbar1:
                        default:
                            loadAnimation(1);
                            break;
                    }
                    return true;
                }
            }
        });
    }

    private void loadAnimation(int i) {
        String svgaString;
        switch(i){
            case 2:
                svgaString = "alarm.svga";
                break;

            case 3:
                svgaString = "angel.svga";
                break;
            case 4:
                svgaString = "EmptyState.svga";
                break;
            case 5:
                svgaString = "heartbeat.svga";
                break;
            case 6:
                svgaString = "posche.svga";
                break;
            case 7:
                svgaString = "rose_1.5.0.svga";
                break;
            case 8:
                svgaString = "rose_2.0.0.svga";
                break;
            case 9:
                svgaString = "test.svga";
                break;
            case 10:
                svgaString = "test2.svga";
                break;
            case 1:
            default:
                svgaString = "shengri.svga";
                break;
        }
        SVGAParser parser = new SVGAParser(SVGAActivity.this);
        parser.decodeFromAssets(svgaString, new SVGAParser.ParseCompletion() {
            @Override
            public void onComplete(@NotNull SVGAVideoEntity videoItem) {
                System.out.println("输出开始");
                SVGADrawable svgaDrawable = new SVGADrawable(videoItem);
                animationView.setImageDrawable(svgaDrawable);
                animationView.startAnimation();

//                animationView.setVideoItem(videoItem);
//                animationView.startAnimation();
                System.out.println("输出结束");
            }
            @Override
            public void onError() {
                MyToast.toastError(SVGAActivity.this,"播放动画出错",false);
            }
        });

    }

}
